package com.example.myquickstart.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myquickstart.entity.Apps;
import com.example.myquickstart.mapper.AppsMapper;
import com.example.myquickstart.service.IAppsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 最基础的增删改查
 * </p>
 *
 * @author Jack
 * @since 2019-05-09
 */
@RestController
@RequestMapping("/study/apps")
public class AppsController {
    @Resource
    private AppsMapper appsMapper;

    @Resource
    private IAppsService appsService;

    @GetMapping("/all")
    public String getAll(){
        List<Apps> apps = appsMapper.myGetAll();
        return JSONUtil.toJsonStr(apps);
    }

    @GetMapping("/page/{current}/{size}")
    public String getPage(@PathVariable("current") int current,@PathVariable("size") int size){
        //page里有两个参数，一个当前页，一个size
        Page<Apps> appsPage = new Page<>(current, size);
        //queryWrapper里加自定义的查询条件
        IPage<Apps> page = appsMapper.selectPage(appsPage, null);
        //序列化
        /*appsIPage结果
        * {"current":1,"pages":2,"
        * records":[{"appName":"QQ APP","country":"CN","url":"http://im.qq.com/"},
        * {"appName":"微博 APP","country":"CN","url":"http://weibo.com/"}],
        * "searchCount":true,"size":2,"total":3}
        * */
        return JSONUtil.toJsonStr(page);
    }

}
